package com.neuedu.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neuedu.dao.User;
import com.neuedu.service.UserService;
import com.neuedu.service.UserServiceImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet2")
public class LoginServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String user = request.getParameter("user");
		String pwd = request.getParameter("pwd");
		
		String str1 = "";
		String str2 = "";
		if(user==null||user.length()==0)
			str1 = "用户名不能为空";
		if(pwd==null||pwd.length()==0)
			str2 = "密码不能为空";
		if(str1.length()>0 || str2.length()>0) {
			request.setAttribute("info", str1+"--"+str2);
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}		
		
		UserService us = new UserServiceImpl();
		User u = us.queryByNamePwd(user, pwd);
		if(u==null) {
			System.out.println("用户名、密码有误，请重新登录");
			response.getWriter().write("用户名、密码有误，请重新登录");
		}else {
			request.getRequestDispatcher("/adminviewServlet").forward(request, response);
		}
				
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
